function e(e) {
    return e && e.__esModule ? e : {
        default: e
    };
}

function t(e) {
    var t = e.userId, n = {
        avatar: e.avatarUrl,
        nickname: e.nickName,
        userId: t
    };
    (0, h.post)(I.default.updateIMUserLogo, n, {
        silentFail: !0
    }).then(function() {}).catch(function() {});
}

function n(e, t) {
    var n = e.avatarUrl, l = e.nickName, d = e.userId;
    k = {
        endpoint: "Applet",
        avatar: n,
        nickname: l,
        companyId: M.envConfig.companyId
    }, O = y.default.getInstance({
        appKey: w,
        account: d,
        autoMarkRead: !1,
        token: t,
        onconnect: o,
        onwillreconnect: r,
        ondisconnect: s,
        onerror: i,
        onroamingmsgs: c,
        onofflinemsgs: a,
        onmsg: u
    });
}

function o() {
    console.log("IM连接成功"), E = !0;
}

function r(e) {
    console.log("IM即将重连"), E = !1;
}

function s(e) {
    if (console.log("IM丢失连接"), E = !1, e) switch (console.log(e), e.code) {
      case 302:
        (0, x.default)({
            content: e,
            type: 8
        });
        break;

      case 417:
        (0, x.default)({
            content: e,
            type: 8
        }), (0, T.showToast)("IM连接失败(417)");
        break;

      case "kicked":
        (0, x.default)({
            content: e,
            type: 8
        }), (0, T.showToast)("IM连接失败(-1)");
    }
}

function i(e) {
    console.log(e);
}

function c(e) {}

function a(e) {
    var t = e.msgs, n = t[0].from;
    C[n] = t, N || (N = {}), N[n] = d(t), R.length && (R.forEach(function(e) {
        return m(e);
    }), R = []), _ && _(N), F && F(N);
}

function u(e) {
    var t = d(e), n = t.from, o = (T.Storage.getSync(S.CARDFRIENDLIST) || []).filter(function(e) {
        return e.cardId === n;
    })[0] || [];
    o.length && 1 == +o.shield || (N && (N[n] || (N[n] = []), N[n].push(t)), H && H(t, e), 
    q && q());
}

function l(e, t) {
    switch (t) {
      case 3:
        if (e.body && "product" === e.body.type) return "【产品】";
        if (e.body && "mall" === e.body.type) return "【商品】";
        break;

      case 4:
        return (e.body && e.body.msg || "").trim();

      case 5:
        return "【优惠券】";

      case 6:
        return "【表单】";
    }
    return "【未知消息类型】";
}

function d(e) {
    var t = Array.isArray(e), n = (t ? e : [ e ]).map(function(e) {
        var t = e.type, n = e.from, o = e.text, r = e.time, s = e.flow, i = e.idClient, c = e.tip, a = {
            from: n,
            type: t,
            time: r,
            flow: s,
            idClient: i,
            showtime: (0, b.showTimer)(r)
        };
        switch (t) {
          case "text":
            return v({}, a, {
                text: o,
                previewContent: (o || "").trim()
            });

          case "image":
            return v({}, a, {
                url: e.file.url,
                w: e.file.w,
                h: e.file.h,
                previewContent: "[图片]"
            });

          case "custom":
            var u = {}, d = -1;
            try {
                4 === (d = +(u = JSON.parse(e.content)).msgType) && "string" == typeof u.body && (u.body = JSON.parse(u.body));
            } catch (e) {}
            return v({}, a, {
                content: u,
                previewContent: l(u, d)
            });

          case "tip":
            return v({}, a, {
                tip: c,
                previewContent: c
            });
        }
        return {};
    });
    return t ? n : n[0];
}

function f(e) {
    L && (0, h.post)(I.default.refreshChatTime, {
        cardId: L
    }).then(function(e) {
        return console.log(e);
    }).catch(function(e) {
        return console.log(e);
    }), O.sendMsgReceipt({
        msg: e,
        done: p
    });
}

function p(e, t) {
    console.log("发送消息已读回执" + (e ? "失败" : "成功"), e, t);
}

function g(e, t, n, o) {
    O.sendCustomMsg({
        isPushable: !0,
        scene: "p2p",
        to: e,
        custom: JSON.stringify(k),
        content: JSON.stringify(t),
        done: function(e, t) {
            e ? o && o() : n && n(t);
        }
    });
}

function m(e) {
    O && (O.markMsgRead(C[e]), N && N[e] && delete N[e]);
}

Object.defineProperty(exports, "__esModule", {
    value: !0
}), exports.createIMUser = exports.clearIMTimeout = exports.setOfflineMsgHeaderLister = exports.setHeaderMsgListfn = exports.changeChatCardId = exports.clearMsgListner = exports.getOnOfflineMsgs = exports.setOfflineMsgLister = exports.resetNoRead = exports.setMsgListner = exports.getMoreHistory = exports.getHistoryList = exports.sendMsgDone = exports.sendProduct = exports.sendCustomMsg = exports.sendTipMsg = exports.sendImgMsg = exports.sendTextMsg = void 0;

var v = Object.assign || function(e) {
    for (var t = 1; t < arguments.length; t++) {
        var n = arguments[t];
        for (var o in n) Object.prototype.hasOwnProperty.call(n, o) && (e[o] = n[o]);
    }
    return e;
}, y = e(require("../../libs/NIM_Web_NIM_v5.5.0")), M = require("../config/index"), x = e(require("../logReport")), h = require("../request"), I = e(require("../urls")), b = require("./time.config"), T = require("../public"), S = require("../storage.key"), w = M.envConfig.IMappKey, O = void 0, N = {}, C = {}, k = {}, L = "", E = !1, P = void 0, R = [], _ = null, F = null, H = null, q = null;

!function() {
    var e = {
        userId: T.Storage.getSync(S.USERINFO_KEY).userId,
        pageNum: 1,
        pageSize: 1e3
    };
    (0, h.post)(I.default.getInvalidCardList, e, {
        silentFail: !0
    }).then(function(e) {
        (R = e.data.data || []).length && Object.keys(N).length && (R.forEach(function(e) {
            N[e] && m(e);
        }), R = [], _ && _(N), F && F(N));
    }).catch(function() {});
}();

var U = T.Storage.getSync(S.IMTOKEN);

U && n(T.Storage.getSync(S.USERINFO_KEY), U), exports.sendTextMsg = function() {
    var e = arguments.length > 0 && void 0 !== arguments[0] ? arguments[0] : {}, t = e.to, n = void 0 === t ? "" : t, o = e.content, r = void 0 === o ? "" : o;
    return new Promise(function(e, t) {
        O.sendText({
            isPushable: !0,
            scene: "p2p",
            to: n,
            text: r,
            custom: JSON.stringify(k),
            done: function(n, o) {
                n ? (console.error(n), t(n)) : (f(o), e(o));
            }
        });
    });
}, exports.sendImgMsg = function() {
    var e = arguments.length > 0 && void 0 !== arguments[0] ? arguments[0] : {}, t = e.to, n = void 0 === t ? "" : t, o = e.url, r = void 0 === o ? "" : o;
    return new Promise(function(e, t) {
        O.sendFile({
            scene: "p2p",
            to: n,
            type: "image",
            wxFilePath: r,
            custom: JSON.stringify(k),
            beginupload: function(e) {},
            uploadprogress: function(e) {
                console.log("文件总大小: " + e.total + "bytes"), console.log("已经上传的大小: " + e.loaded + "bytes"), 
                console.log("上传进度: " + e.percentage), console.log("上传进度文本: " + e.percentageText);
            },
            uploaddone: function(e, n) {
                console.error(e), console.log("上传" + (e ? "失败" : "成功")), e && t(e);
            },
            beforesend: function(e) {
                console.log("正在发送p2p image消息, id=" + e.idClient);
            },
            done: function(t) {
                e(!1);
            }
        });
    });
}, exports.sendTipMsg = function() {
    var e = arguments.length > 0 && void 0 !== arguments[0] ? arguments[0] : {}, t = e.to, n = void 0 === t ? "" : t, o = e.tip, r = void 0 === o ? "" : o;
    return new Promise(function(e, t) {
        O.sendTipMsg({
            isPushable: !0,
            scene: "p2p",
            to: n,
            tip: r,
            custom: JSON.stringify(k),
            done: function(n, o) {
                n ? (console.error(n), t(n)) : (f(o), e(o));
            }
        });
    });
}, exports.sendCustomMsg = g, exports.sendProduct = function() {
    var e = arguments.length > 0 && void 0 !== arguments[0] ? arguments[0] : {}, t = e.name, n = void 0 === t ? "" : t, o = e.to, r = void 0 === o ? "" : o, s = e.context, i = void 0 === s ? 0 : s, c = e.proImg, a = void 0 === c ? "" : c, u = e.bsId, l = void 0 === u ? "" : u, d = e.proTitle, f = void 0 === d ? "" : d, p = e.type, m = void 0 === p ? "" : p, v = e.success, y = void 0 === v ? null : v, M = e.fail;
    g(r, {
        msgType: 3,
        body: {
            name: n,
            pro_title: f,
            pro_img: a,
            bs_id: l,
            context: i,
            type: m
        }
    }, y, void 0 === M ? null : M);
}, exports.sendMsgDone = f, exports.getHistoryList = function(e) {
    return new Promise(function(t, n) {
        function o() {
            if (E) return (0, b.initStartTime)(), void O.getHistoryMsgs({
                scene: "p2p",
                asc: !0,
                to: e,
                limit: 15,
                done: function(e, o) {
                    if (e) n(new Error("获取历史消息失败")); else {
                        var r = o.msgs;
                        O.markMsgRead(r), f(r[r.length - 1]), t(d(r));
                    }
                }
            });
            clearTimeout(P), P = setTimeout(o, 1e3);
        }
        o();
    });
}, exports.getMoreHistory = function(e, t) {
    return new Promise(function(n, o) {
        (0, b.initStartTime)(), O.getHistoryMsgs({
            scene: "p2p",
            asc: !0,
            to: e,
            endTime: t,
            limit: 15,
            done: function(e, t) {
                e ? o(new Error("获取历史消息失败")) : n(d(t.msgs));
            }
        });
    });
}, exports.setMsgListner = function(e) {
    H = e;
}, exports.resetNoRead = m, exports.setOfflineMsgLister = function(e) {
    _ = e;
}, exports.getOnOfflineMsgs = function() {
    return (T.Storage.getSync(S.CARDFRIENDLIST) || []).forEach(function(e) {
        1 == +e.shield && m(e.cardId);
    }), N;
}, exports.clearMsgListner = function() {
    H = null;
}, exports.changeChatCardId = function(e) {
    L = e;
}, exports.setHeaderMsgListfn = function(e) {
    q = e;
}, exports.setOfflineMsgHeaderLister = function(e) {
    F = e;
}, exports.clearIMTimeout = function() {
    clearTimeout(P);
}, exports.createIMUser = function(e) {
    var o = e.userId;
    (0, h.get)(I.default.createIMuserUrl, {
        endpoint: "Applet",
        userId: o
    }, {
        silentFail: !0
    }).then(function(o) {
        var r = o.data.data;
        T.Storage.set(S.IMTOKEN, r), n(e, r), t(e);
    }).catch(function() {
        (0, h.post)(I.default.updateIMToken, {
            userId: o
        }, {
            silentFail: !0
        }).then(function(o) {
            var r = o.data.data;
            T.Storage.set(S.IMTOKEN, r), n(e, r), t(e);
        }).catch(function() {});
    });
};